﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Net.Mail;
using System.Data.SqlClient;
using System.Configuration;
using System.Data;
using System.IO;


namespace MIT.DivAdmin
{
    public partial class DivAdminReInvite3 : System.Web.UI.Page
    {


        string[] getlistname = new string[1];
        string[] getlistemail = new string[1];
        string name1 = null;


        string email1 = null;


        string request1 = null;
        string request2 = null;
        string request3 = null;
        string Name = null;
        string Division1 = null;

        int GlobalInterviewNo;
        String GlobalInterviewDate;
        String GlobalSender3;


        String Req1;
        String Req2;
        String Req3;
        String NewsDate1;
        String NewsDate2;
        String NewsDate3;
        String NewsTitle1;
        String NewsTitle2;
        String NewsTitle3;
        String NewsDetails1;
        String NewsDetails2;
        String NewsDetails3;

        protected void Page_Load(object sender, EventArgs e)
        {
            String Name = (String)Session["LoggingInfo"];
            String Position = (String)Session["LoggingPosition"];
            String username = (String)Session["LoggingInfoUsername"];
            LabelDivAdminName.Text = Name;
            LabelDivAdminPosition.Text = "(" + Position + ")";

            ImageProfile.ImageUrl = "../ProfilePicHandler.ashx?uname=" + username;

            Session["CalLoggingInfo"] = Name;
            Session["CalLoggingPosition"] = Position;
            Session["CalLoggingInfoUsername"] = username;

            ///////////////////Notification Number//////////////////////////////////////////////////
            string CS = ConfigurationManager.ConnectionStrings["DBCS"].ConnectionString;

            using (SqlConnection con = new SqlConnection(CS))
            {
                SqlCommand ReqMsgNo = new SqlCommand("spMsgCounter", con);
                ReqMsgNo.CommandType = CommandType.StoredProcedure;

                SqlParameter ReqName3 = new SqlParameter("@username", username);
                ReqMsgNo.Parameters.Add(ReqName3);



                con.Open();
                SqlDataReader dr;

                dr = ReqMsgNo.ExecuteReader();

                if (dr.Read())
                {
                    Req1 = Convert.ToString(dr["REQUEST1"]);
                    Req2 = Convert.ToString(dr["REQUEST2"]);
                    Req3 = Convert.ToString(dr["REQUEST3"]);
                }

            }
            if (Req1 == "")
            {
                LabelMsgNo.Text = "";

            }
            else if (Req2 == "")
            {
                LabelMsgNo.Text = "1";
            }
            else if (Req3 == "")
            {
                LabelMsgNo.Text = "2";
            }
            else
            {
                LabelMsgNo.Text = "3";
            }


            using (SqlConnection con = new SqlConnection(CS))
            {
                SqlCommand GetNews = new SqlCommand("spGetNews1", con);
                GetNews.CommandType = CommandType.StoredProcedure;

                SqlParameter News1 = new SqlParameter("@News1", 1);
                GetNews.Parameters.Add(News1);



                con.Open();
                SqlDataReader dr;

                dr = GetNews.ExecuteReader();

                if (dr.Read())
                {
                    NewsDate1 = Convert.ToString(dr["LiveDate"]);
                    NewsTitle1 = Convert.ToString(dr["Title"]);
                    NewsDetails1 = Convert.ToString(dr["Details"]);

                }

            }

            using (SqlConnection con = new SqlConnection(CS))
            {
                SqlCommand GetNews = new SqlCommand("spGetNews2", con);
                GetNews.CommandType = CommandType.StoredProcedure;

                SqlParameter News2 = new SqlParameter("@News2", 2);
                GetNews.Parameters.Add(News2);



                con.Open();
                SqlDataReader dr;

                dr = GetNews.ExecuteReader();

                if (dr.Read())
                {
                    NewsDate2 = Convert.ToString(dr["LiveDate"]);
                    NewsTitle2 = Convert.ToString(dr["Title"]);
                    NewsDetails2 = Convert.ToString(dr["Details"]);

                }

            }

            using (SqlConnection con = new SqlConnection(CS))
            {
                SqlCommand GetNews = new SqlCommand("spGetNews3", con);
                GetNews.CommandType = CommandType.StoredProcedure;

                SqlParameter News3 = new SqlParameter("@News3", 3);
                GetNews.Parameters.Add(News3);



                con.Open();
                SqlDataReader dr;

                dr = GetNews.ExecuteReader();

                if (dr.Read())
                {
                    NewsDate3 = Convert.ToString(dr["LiveDate"]);
                    NewsTitle3 = Convert.ToString(dr["Title"]);
                    NewsDetails3 = Convert.ToString(dr["Details"]);
                }

            }



            LabelDetails1.Text = NewsDetails1;
            LabelDetails2.Text = NewsDetails2;
            LabelDetails3.Text = NewsDetails3;

            LabelNews1.Text = NewsTitle1;
            LabelNews2.Text = NewsTitle2;
            LabelNews3.Text = NewsTitle3;

            LabelDate1.Text = NewsDate1;
            LabelDate2.Text = NewsDate2;
            LabelDate3.Text = NewsDate3;


            String Interviewer1 = (String)Session["Interviewer1"];
            String Interviewer2 = (String)Session["Interviewer2"];
            String Interviewer3 = (String)Session["Interviewer3"];
            int InterviewNo = (int)Session["InterviewNo"];
            String IntervieweDate = (String)Session["IntervieweDate"];
            String Sender3 = (String)Session["Sender3"];

            GlobalInterviewNo = InterviewNo;
            GlobalInterviewDate = IntervieweDate;
            GlobalSender3 = Sender3;

            Label1.Text = Interviewer1;
            Label2.Text = Interviewer2;
            Label3.Text = Interviewer3;

            Label1.Visible = false;
            Label2.Visible = false;
            Label3.Visible = false;

            //LabelDivHeadName.Text = Name;
            //LabelPosition.Text = Position;

            //ImageProfile.ImageUrl = "../ProfilePicHandler.ashx?uname=" + username;

            if (!IsPostBack)
            {
                CheckBoxListInterviewPanel.SelectedIndex = 0;
            }




        }



        protected void ButtonStep2_Click1(object sender, EventArgs e)
        {

            int i = 0;
            foreach (ListItem li in CheckBoxListInterviewPanel.Items)
            {


                if (li.Selected)
                {

                    getlistname[i] = li.Text;
                    i++;
                }
            }

            if (getlistname[0] == null)
            {
                LabelValidation.Text = "Select aleast one Interviwers";
            }

            else
            {
                string CS = ConfigurationManager.ConnectionStrings["DBCS"].ConnectionString;

                using (SqlConnection con = new SqlConnection(CS))
                {
                    ///////////////////////////////////////////////////////////////////////////////////////////////////////////
                    ///////////////////////////////////////////////////////////////////////////////////////////////////////////
                    /////////////////User 1////////////////////////////////////////////////////////////////////////////////////
                    if (getlistname[0] != null)
                    {
                        SqlCommand com = new SqlCommand("spGetName1", con);
                        com.CommandType = CommandType.StoredProcedure;

                        SqlParameter sendname1 = new SqlParameter("@Name1", getlistname[0]);
                        com.Parameters.Add(sendname1);



                        con.Open();
                        SqlDataReader dr;

                        dr = com.ExecuteReader();

                        if (dr.Read())
                        {
                            name1 = dr["USERNAME"].ToString();
                        }
                        con.Close();


                        SqlCommand com2 = new SqlCommand("spGetEmail1", con);
                        com2.CommandType = CommandType.StoredProcedure;

                        SqlParameter sendname2 = new SqlParameter("@UserName1", name1);
                        com2.Parameters.Add(sendname2);



                        con.Open();
                        SqlDataReader dr1;

                        dr1 = com2.ExecuteReader();

                        if (dr1.Read())
                        {
                            email1 = dr1["EMAIL"].ToString();
                        }
                        /////////////////////////////////////////////////////////////////////////////////////////////
                        ////////////////////////////////////////////////////////////////////////////////////////////
                        con.Close();


                        SqlCommand ReqCom1 = new SqlCommand("spRequestchecker1", con);
                        ReqCom1.CommandType = CommandType.StoredProcedure;

                        SqlParameter ReqName1 = new SqlParameter("@username", name1);
                        ReqCom1.Parameters.Add(ReqName1);



                        con.Open();
                        SqlDataReader drreq1;

                        drreq1 = ReqCom1.ExecuteReader();

                        if (drreq1.Read())
                        {
                            request1 = drreq1["REQUEST1"].ToString();
                        }
                        ///////////////////////////////////////////////////////////////////////////////////////////////
                        con.Close();


                        SqlCommand ReqCom2 = new SqlCommand("spRequestchecker2", con);
                        ReqCom2.CommandType = CommandType.StoredProcedure;

                        SqlParameter ReqName2 = new SqlParameter("@username", name1);
                        ReqCom2.Parameters.Add(ReqName2);



                        con.Open();
                        SqlDataReader drreq2;

                        drreq2 = ReqCom2.ExecuteReader();

                        if (drreq2.Read())
                        {
                            request2 = drreq2["REQUEST2"].ToString();
                        }
                        //////////////////////////////////////////////////////////////////////////////////////////////
                        con.Close();


                        SqlCommand ReqCom3 = new SqlCommand("spRequestchecker3", con);
                        ReqCom3.CommandType = CommandType.StoredProcedure;

                        SqlParameter ReqName3 = new SqlParameter("@username", name1);
                        ReqCom3.Parameters.Add(ReqName3);



                        con.Open();
                        SqlDataReader drreq3;

                        drreq3 = ReqCom3.ExecuteReader();

                        if (drreq3.Read())
                        {
                            request3 = drreq3["REQUEST3"].ToString();
                        }

                        if (request1 == "")
                        {
                            SqlCommand cmd = new SqlCommand("spAddRequest1", con);
                            cmd.CommandType = System.Data.CommandType.StoredProcedure;
                            cmd.Parameters.AddWithValue("@UsernameReq1", name1);
                            cmd.Parameters.AddWithValue("@Sender1", Name);
                            cmd.Parameters.AddWithValue("@DIVISION1", Division1);
                            cmd.Parameters.AddWithValue("@Req1", ("Interview No. " + GlobalInterviewNo + ". You have an interview panel invitation on " + GlobalInterviewDate));
                            con.Close();
                            con.Open();
                            cmd.ExecuteNonQuery();
                            con.Close();
                        }

                        else if (request2 == "")
                        {
                            SqlCommand cmd = new SqlCommand("spAddRequest2", con);
                            cmd.CommandType = System.Data.CommandType.StoredProcedure;
                            cmd.Parameters.AddWithValue("@Req2", ("Interview No. " + GlobalInterviewNo + ". You have an interview panel invitation on " + GlobalInterviewDate));
                            cmd.Parameters.AddWithValue("@Sender2", Name);
                            cmd.Parameters.AddWithValue("@DIVISION2", Division1);
                            cmd.Parameters.AddWithValue("@UsernameReq2", name1);
                            con.Close();
                            con.Open();
                            cmd.ExecuteNonQuery();
                            con.Close();
                        }

                        else if (request3 == "")
                        {
                            SqlCommand cmd = new SqlCommand("spAddRequest3", con);
                            cmd.CommandType = System.Data.CommandType.StoredProcedure;
                            cmd.Parameters.AddWithValue("@Req3", ("Interview No. " + GlobalInterviewNo + ". You have an interview panel invitation on " + GlobalInterviewDate));
                            cmd.Parameters.AddWithValue("@Sender3", Name);
                            cmd.Parameters.AddWithValue("@DIVISION3", Division1);
                            cmd.Parameters.AddWithValue("@UsernameReq3", name1);
                            con.Close();
                            con.Open();
                            cmd.ExecuteNonQuery();
                            con.Close();
                        }
                        else
                        {
                            LabelRequestMax.Text = getlistname[0] + " has too many requests. Pick Another";
                            goto EndofPage;
                        }


                        //////////////////////////////////////////////////////////////////////////////////////////
                        //////////////////////////////////////////////////////////////////////////////////////////

                        if (Page.IsValid)
                            try
                            {
                                MailMessage mm = new MailMessage("cms.mit2015@gmail.com", email1);
                                //mm.From = new MailAddress();
                                //mm.To.Add();
                                mm.Subject = "Interview Panel Invitation";

                                mm.Body = "Hello Test 1";

                                mm.IsBodyHtml = true;

                                SmtpClient smtpClient = new SmtpClient("smtp.gmail.com", 587);
                                smtpClient.EnableSsl = true;
                                smtpClient.Credentials = new System.Net.NetworkCredential()
                                {
                                    UserName = "cms.mit2015@gmail.com",
                                    Password = "millenium2015"
                                };
                                smtpClient.Send(mm);


                            }

                            catch (Exception ex)
                            {
                                Response.Write("Could not send the e-mail - error: " + ex.Message);
                            }


                        con.Close();
                    }

                }
                string CSs = ConfigurationManager.ConnectionStrings["DBCS"].ConnectionString;

                using (SqlConnection con = new SqlConnection(CSs))
                {
                    SqlCommand cmd1 = new SqlCommand("spAddOneInterviewer", con);
                    cmd1.CommandType = System.Data.CommandType.StoredProcedure;
                    cmd1.Parameters.AddWithValue("@INTERVIEW_NO", GlobalInterviewNo);
                    cmd1.Parameters.AddWithValue("@INTERVIEWER1", getlistname[0]);
                    cmd1.Parameters.AddWithValue("@SENDER1", GlobalSender3);
                    con.Close();
                    con.Open();
                    cmd1.ExecuteNonQuery();

                    LabelInterviewStatus.Text = "Schedule Successful";

                    Response.Redirect("DivAdminTools.aspx");
                }
            EndofPage: ;
            }

        }
    }
}